package com.example.resultinterceptor.mapper;

import com.example.resultinterceptor.entity.po.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    /**
     * 根据ID查找用户
     */
    @Select("SELECT * FROM users WHERE id = #{id}")
    User findById(@Param("id") Long id);

    /**
     * 根据用户名查找用户
     */
    @Select("SELECT * FROM users WHERE username = #{username}")
    User findByUsername(@Param("username") String username);

    /**
     * 根据邮箱查找用户
     */
    @Select("SELECT * FROM users WHERE email = #{email}")
    User findByEmail(@Param("email") String email);

    /**
     * 查找所有激���用户
     */
    @Select("SELECT * FROM users WHERE status = 1")
    List<User> findActiveUsers();

    /**
     * 根据昵称模糊查询
     */
    @Select("SELECT * FROM users WHERE nickname LIKE #{nickname}")
    List<User> findByNicknameLike(@Param("nickname") String nickname);

    /**
     * 查找所有用户
     */
    @Select("SELECT * FROM users")
    List<User> findAll();

    /**
     * 保存用户
     */
    @Insert("INSERT INTO users (username, password, email, phone, nickname, avatar, status, create_time, update_time) " +
            "VALUES (#{username}, #{password}, #{email}, #{phone}, #{nickname}, #{avatar}, #{status}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertUser(User user);

    /**
     * 保存用户并返回用户对象
     */
    default User save(User user) {
        insertUser(user);
        return user;
    }

    /**
     * 更新用户
     */
    @Update("UPDATE users SET username = #{username}, password = #{password}, email = #{email}, " +
            "phone = #{phone}, nickname = #{nickname}, avatar = #{avatar}, status = #{status}, " +
            "update_time = #{updateTime} WHERE id = #{id}")
    int update(User user);

    /**
     * 根据ID删除用户
     */
    @Delete("DELETE FROM users WHERE id = #{id}")
    int deleteById(@Param("id") Long id);

    // ========== JDBC 报错示例方法 ==========

    /**
     * 测试SQL语法错误 - 故意写错误的SQL
     */
    @Select("SELECTT * FROMM users")  // 故意拼写错误
    List<User> testSqlSyntaxError();

    /**
     * 测试表不存在错误
     */
    @Select("SELECT * FROM non_existent_table")
    List<User> testTableNotExistsError();

    /**
     * 测试字段不存在错误
     */
    @Select("SELECT non_existent_column FROM users")
    List<User> testColumnNotExistsError();

    /**
     * 测试违反唯一约束错误
     */
    @Insert("INSERT INTO users (username, email, create_time, update_time) " +
            "VALUES (#{username}, #{email}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertDuplicateUser(User user);

    /**
     * 测试违反唯一约束错误的包装方法
     */
    default User testDuplicateKeyError(User user) {
        insertDuplicateUser(user);
        return user;
    }

    /**
     * 测试数据库连接超时错误 - 使用复杂查询模拟超时
     */
    @Select("SELECT * FROM users WHERE id IN (SELECT id FROM users WHERE id IN (SELECT id FROM users))")
    List<User> testConnectionTimeoutError();

    /**
     * 测试死锁错误 - 返回字符串结果
     */
    @Select("SELECT 'deadlock simulation' as result")
    String testDeadlockError();

    /**
     * 测试PostgreSQL特定异常
     */
    @Select("SELECT * FROM users WHERE id = 'invalid_id_type'")
    List<User> testPSQLException();
}
